/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MapButton.java 607 2010-07-27 07:16:34Z Harry $
 *   $LastChangedDate: 2010-07-27 07:16:34 +0000 (Tue, 27 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.resource.Graphics;
import battlefield.entity.Cell;
import battlefield.entity.UnitCell;
import battlefield.state.CellState;
import battlefield.util.ImageUtils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 *
 * @author Harry Nguyen
 */
public class MapButton extends JButton {

    private int row;
    private int column;
    private boolean unitVisibility;
    private boolean errorVisibility;
    private boolean preview;
    private Cell cell;
    private Cell previewCell;

    public MapButton() {
        this.setBorder(null);
        errorVisibility = true;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public Cell getCell() {
        return cell;
    }

    public void setCell(Cell cell) {
        if (cell == null) {
            this.cell = null;
            setIcon(null);
        } else if (!cell.equals(this.cell)) {
            this.cell = cell;
            update();
        }
    }

    public Cell getPreviewCell() {
        return previewCell;
    }

    public void setPreviewCell(Cell previewCell) {
        this.previewCell = previewCell;
    }

    public void update() {
        Image image = this.getImage(cell);
        ImageIcon icon = new ImageIcon(image);
        this.setIcon(icon);
    }

    public boolean isUnitVisibility() {
        return unitVisibility;
    }

    public void setUnitVisibility(boolean unitVisibility) {
        this.unitVisibility = unitVisibility;
    }

    public boolean isErrorVisibility() {
        return errorVisibility;
    }

    public void setErrorVisibility(boolean errorVisibility) {
        this.errorVisibility = errorVisibility;
    }

    public boolean isPreview() {
        return preview;
    }

    public void setPreview(boolean preview) {
        this.preview = preview;
    }

    public Image getImage(Cell cell) {
        BufferedImage image = ImageUtils.newBufferedImage(Cell.WIDTH, Cell.HEIGHT);

        ImageUtils.overlayImage(image, Graphics.get(cell.getBackgroundImage()).getBufferedImage());

        if (unitVisibility && cell instanceof UnitCell) {
            ImageUtils.overlayImage(image, Graphics.get(((UnitCell) cell).getUnitImage()).getBufferedImage());
        }

        if (cell.getState() == CellState.DESTROYED) {
            ImageUtils.overlayImage(image, Graphics.get(cell.getDestroyedImage()).getBufferedImage());
        }

        if (preview && previewCell instanceof UnitCell) {
            if ((unitVisibility && cell instanceof UnitCell) || (cell.getState() == CellState.DESTROYED)) {
                ImageUtils.overlayImage(image, Graphics.get(((UnitCell) previewCell).getUnitImage()).getBufferedImage());
                if (cell.getState() == CellState.DESTROYED) {
                    ImageUtils.overlayImage(image, Graphics.get(cell.getDestroyedImage()).getBufferedImage());
                }
                if (isErrorVisibility()) {
                    ImageUtils.overlayImage(image, Graphics.ICON_PREVIEW_ERROR.getBufferedImage());
                }
            } else {
                ImageUtils.overlayImage(image, Graphics.get(((UnitCell) previewCell).getUnitImage()).getBufferedImage());
            }
        }

        return image;
    }
}
